package com.hrms.repository;

import com.hrms.entity.SalaryRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Repository
public interface SalaryRecordRepository extends JpaRepository<SalaryRecord, Long> {
    
    Optional<SalaryRecord> findByEmployeeIdAndSalaryMonth(Long employeeId, LocalDate salaryMonth);
    
    List<SalaryRecord> findByEmployeeIdOrderBySalaryMonthDesc(Long employeeId);
    
    @Query("SELECT s FROM SalaryRecord s WHERE " +
           "(:employeeId IS NULL OR s.employee.id = :employeeId) AND " +
           "(:departmentId IS NULL OR s.employee.department.id = :departmentId) AND " +
           "s.salaryMonth BETWEEN :startDate AND :endDate")
    Page<SalaryRecord> findByFilters(@Param("employeeId") Long employeeId,
                                     @Param("departmentId") Long departmentId,
                                     @Param("startDate") LocalDate startDate,
                                     @Param("endDate") LocalDate endDate,
                                     Pageable pageable);
}
